package com.iraeta.controller.basedata;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.Caizhi;
import com.iraeta.service.CaiZhiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController(value = "CaiZhiController")
@RequestMapping("caizhi")
public class CaiZhiController extends BaseController {
    @Autowired
    CaiZhiService caiZhiService;

    @RequestMapping("pagecaizhi")
    public String pageCaiZhi(@RequestBody JSONObject jo){
        String cname = jo.getString("cname");
        this.pageSet(jo);
        List<Caizhi> caizhiList = caiZhiService.listCaiZhi(cname);
        PageInfo<Caizhi> caizhiPageInfo = this.getPageInfo(caizhiList);
        return this.success(caizhiPageInfo);
    }

    @RequestMapping("getcaizhi")
    public String getCaiZhi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Caizhi caizhi = caiZhiService.getCaiZhi(id);
        return this.success(caizhi);
    }

    @RequestMapping("addcaizhi")
    public String addEditCaiZhi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        Caizhi caizhi = id == 0 ? new Caizhi() :  caiZhiService.getCaiZhi(id);
        this.mapJsonToObj(paramJb, caizhi);
        caizhi.setCdefine1(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_FORMAT));
        if(id == 0){
            caizhi.setDeleted(0);
            caiZhiService.addCaiZhi(caizhi);
        }else {
            caiZhiService.updateCaiZhi(caizhi);
        }
        return this.success();
    }

    @RequestMapping("deletecaizhi")
    public String deleteCaiZhi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        caiZhiService.deleteCaiZhi(id);
        return this.success();
    }

    @RequestMapping("listallcaizhi")
    public String listAllCaiZhi(){
        List<Caizhi> caizhiList = caiZhiService.listAllCaiZhi();
        return this.success(caizhiList);
    }

    /*查看某一种材质的历史价格*/
    @RequestMapping("listhiscaizhi")
    public String listHisCaiZhi(@RequestBody JSONObject paramJb){
        int id = paramJb.getIntValue("id");
        List<Caizhi> caizhiList = caiZhiService.listHisCaiZhi(id);
        return this.success(caizhiList);
    }
}
